export default class Overlay {
  get visible () {
    return this.overlay.show
  }

  set visible (bool) {
    this.overlay.show = bool
  }

  destroy () {
    if (this.overlay instanceof GC.Entity && this.sandbox.viewer.entities.contains(this.overlay)) {
      this.sandbox.viewer.entities.remove(this.overlay)
    } else if (typeof this.overlay.destroy === 'function' && this.sandbox.viewer.scene.primitives.contains(this.overlay)) {
      this.sandbox.viewer.scene.primitives.remove(this.overlay)
    } else {
      console.error('没有销毁方法')
    }
  }

  on (type, callback, option) {
    const handlerOfType = this.sandbox.eventMap.get(type)
    if (handlerOfType) {
      if (!handlerOfType.has(this.overlay)) {
        handlerOfType.set(this.overlay, new Map())
      }
      const callbackMap = handlerOfType.get(this.overlay)
      callbackMap.set(callback, { option })
    } else {
      console.error('事件类型有误')
    }
  }

  off (type, callback) {
    const handlerOfType = this.sandbox.eventMap.get(type)
    if (handlerOfType) {
      const callbackMap = handlerOfType.get(this.overlay)
      if (callbackMap) {
        callbackMap.delete(callback)
        if (!callbackMap.size) {
          handlerOfType.delete(this.overlay)
        }
      } else {
        console.error('不存在要销毁的回调函数')
      }
    } else {
      console.error('事件类型有误')
    }
  }

  modelToWorldPosition (input) {
    if (!this.modelMatrix) {
      return undefined
    }
    const modelPosition = new GC.Cartesian3(...input.slice(0, 3))
    const worldPosition = GC.Matrix4.multiplyByPoint(
      this.modelMatrix,
      modelPosition,
      new GC.Cartesian3()
    )
    return worldPosition
  }

  worldToModelPosition (input) {
    if (!this.modelMatrixInverse) {
      return undefined
    }
    const worldPosition = new GC.Cartesian3(...input.slice(0, 3))
    const modelPosition = GC.Matrix4.multiplyByPoint(
      this.modelMatrixInverse,
      worldPosition,
      new GC.Cartesian3()
    )
    return modelPosition
  }
}
